package com.greedy.bulls.stock.gateway.filters;

import io.netty.util.internal.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-07-30
 * Description: 全局过滤器
 */
@Log4j2
@Component
public class StockRequestGlobalFilter implements GlobalFilter, Ordered {


    /**
     * 通过filter来自定义配置转发信息
     *
     * @param exchange 服务请求对象
     * @param chain    过滤器链
     * @return Mono
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String authentication = exchange.getRequest().getHeaders().getFirst("Authorization");
        if (!StringUtil.isNullOrEmpty(authentication)) {
            log.info("enter stockRequestGlobalFilter filter method: " + authentication);
            exchange.getRequest().mutate().header("Authorization", authentication);
        }
        return chain.filter(exchange.mutate().build());
    }

    @Override
    public int getOrder() {
        return -1000;
    }
}

